﻿using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace zijian666.Http;

/// <summary>
/// 表示一个表单数据
/// </summary>
public readonly struct FormData
{
    /// <summary>
    /// 表单数据名称
    /// </summary>
    public readonly string Name;
    /// <summary>
    /// 表单数据的值
    /// </summary>
    public readonly HttpContent Value;

    /// <summary>
    /// 创建一个表单数据
    /// </summary>
    /// <param name="name">表单数据名称</param>
    /// <param name="value">表单数据的值</param>
    public FormData(string name, HttpContent value)
    {
        Value = value;
        Name = name;
    }
    /// <summary>
    /// 创建一个字符串类型的表单数据
    /// </summary>
    /// <param name="name">表单数据名称</param>
    /// <param name="value">字符串</param>
    /// <param name="encoding">字符串的编码格式</param>
    public FormData(string name, string value, Encoding encoding = null)
        : this(name, new StringContent(value, encoding ?? Encoding.UTF8))
    {
    }


    /// <summary>
    /// 创建一个文件类型的表单数据
    /// </summary>
    /// <param name="name">表单数据名称</param>
    /// <param name="file">文件</param>
    /// <param name="contentType">文件类型</param>
    /// <param name="contentDisposition">文件描述</param>
    public FormData(string name, FileInfo file, string contentType = null, string contentDisposition = null)
        : this(name, File.ReadAllBytes(file.FullName), file.Name, contentType, contentDisposition)
    {
    }
    /// <summary>
    /// 创建一个文件类型的表单数据
    /// </summary>
    /// <param name="name">表单数据名称</param>
    /// <param name="data">文件数据流</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="contentType">文件类型</param>
    /// <param name="contentDisposition">文件描述</param>
    public FormData(string name, Stream data, string fileName, string contentType = null, string contentDisposition = null)
    {
        var content = new StreamContent(data);
        if (contentType is not null)
        {
            content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
        }
        else
        {
            content.Headers.ContentType = MediaTypes.Mapping(fileName);
        }

        if (contentDisposition is null)
        {
            contentDisposition = $"form-data; name=\"{name}\"; filename=\"{fileName}\"";
        }

        content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);

        Value = content;
        Name = name;
    }
    /// <summary>
    /// 创建一个文件类型的表单数据
    /// </summary>
    /// <param name="name">表单数据名称</param>
    /// <param name="bytes">文件内容字节</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="contentType">文件类型</param>
    /// <param name="contentDisposition">文件描述</param>
    public FormData(string name, byte[] bytes, string fileName, string contentType = null, string contentDisposition = null)
    {
        var content = new ByteArrayContent(bytes);
        if (contentType is not null)
        {
            content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
        }
        else
        {
            content.Headers.ContentType = MediaTypes.Mapping(fileName);
        }

        if (contentDisposition is null)
        {
            contentDisposition = $"form-data; name=\"{name}\"; filename=\"{fileName}\"";
        }

        content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);

        Value = content;
        Name = name;
    }
}
